package cn.aj.basic.service.impl;

import cn.aj.basic.mapper.BaseMapper;
import cn.aj.basic.query.BaseQuery;
import cn.aj.basic.service.BaseService;
import cn.aj.basic.utils.PageListUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public List<T> loadAll() {
        List<T> list = baseMapper.loadAll();
        return list;
    }

    @Override
    public T getById(Long id) {
        return baseMapper.getById(id);
    }

    @Override
    public void add(T t) {
        baseMapper.add(t);

    }


    @Override
    public void delete(Long id) {
        baseMapper.delete(id);

    }

    @Override
    public void batchDelete(List<Long> ids) {
        baseMapper.batchDelete(ids);

    }

    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public PageListUtil<T> loadPage(BaseQuery baseQuery) {
        // 查询总条数
        Integer totals = baseMapper.loadTotal(baseQuery);
        // 判断总条数
        if (null == totals || 0 == totals) { // 判断为null或者为0
            return new PageListUtil<T>(0, new ArrayList<>()); // 返回数据给前端[]
        }
        // 查询分页
        List<T> rows = baseMapper.loadPage(baseQuery);
        return new PageListUtil<T>(totals, rows); // 返回totals和rows
    }

}
